package formatter;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class Table2XML {
	public static void formatData(String fileName) {
		File file = new File(fileName);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;
			// 一次读入一行，直到读入null为文件结束
			while ((tempString = reader.readLine()) != null) {
				String[] map = tempString.split("[\\t]+");
				if (map.length < 2) {
					System.out.println();
					continue;
				}
				
				String line = String.format(
						"<%s type=\"%s\" comment=\"%s\" />", map[0],
						convertType(map[1]), map.length == 2 ? "" : map[2]);
				System.out.println(line);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	public static String convertType(String type) {
		String lowerCase = type.toLowerCase();
		/*
		 * if (lowerCase.contains("varchar")) { return "String"; } else
		 */
		if (lowerCase.contains("number")) {
			return "int";
		} else if (lowerCase.contains("date")) {
			return "Date";
		}

		return "String";
	}

	public static void main(String[] args) {
		String fileName = "./src/data3.txt";

		formatData(fileName);
	}
}
